package org.aeon.service

import org.aeon.dao.UserDao
import org.aeon.entity.User

/**
 * @author:     MissingAeon
 * @date:       2024/1/18 16:55
 * @description: 注册相关业务
 */
class Register {
    private val userDao: UserDao = UserDao()

    fun prepare() {
        println("请输入要注册的账号:")
        val username = readln()
        if (!inspect(username)) {
            println("账号已经被使用，请重新注册!")
            // TODO 这里可能溢出
            prepare()
            return
        }
        println("请输入要注册的密码:")
        // TODO 隐藏密码的输入
        val password = readln()
        register(username, password)

    }

    private fun inspect(username: String): Boolean = !userDao.exist(username)

    private fun register(username: String, password: String) {
        if (userDao.insert(User(username, password))) {
            println("注册成功!")
        } else
            println("注册失败")
    }

}
